package img

/*
#include "LayoutAnalysis.h"

// intentionally write the same LDFLAGS differently
#cgo CFLAGS : -I.
#cgo linux LDFLAGS: -L. -lLayout -lstdc++
#cgo darwin LDFLAGS: -L. -lLayout -lstdc++
*/
import "C"

import (
	// "bytes"
	"fmt"
	"image"
	"image/draw"
	// "image/png"
	// "unsafe"
)

func getImgLineBySO(img image.Image) (result [][]int) {
	// CpbImage
	size := img.Bounds()
	// var pic image.Image
	pic := image.NewGray(size)
	draw.Draw(pic, size, img, img.Bounds().Min, draw.Src)
	maxlength := 1000
	// buf := new(bytes.Buffer)
	// err := png.Encode(buf, pic)
	// fmt.Println(err)
	// send_s3 := buf.Bytes()

	Cwidth := C.int(size.Max.X)
	Cheight := C.int(size.Max.Y)
	Cresult := make([]C.int, maxlength)
	CpbImage := make([]C.uchar, size.Max.X*size.Max.Y)
	fmt.Println("in GetImgLineBySO", len(pic.Pix), len(CpbImage), Cwidth, Cheight)
	for k, v := range pic.Pix {
		CpbImage[k] = C.uchar(v)
	}
	count := C.hw_SegmentHomework(&Cresult[0], C.int(maxlength), &CpbImage[0], Cwidth, Cheight)
	fmt.Println("result", count, Cresult)

	for i := 0; i < int(count); i++ {
		var tmpArr []int
		for j := 0; j < 4; j++ {
			tmpArr_tmp := int(Cresult[i*4+j])
			tmpArr = append(tmpArr, tmpArr_tmp)
		}
		result = append(result, tmpArr)
	}
	fmt.Println("result", result)
	return
}
